package com.lfy.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.deepoove.poi.XWPFTemplate;
import com.lfy.conf.MinioConfig;
import com.lfy.util.MinioUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;

@Controller
public class upload {

   private static final String accessKey="LTAI5tJqdgtaBiXVaSiiKNPD";
    private static final String secretKey="YnburiHKXPFfTi5ie67sOA1L5zjgAB";
    private static final String bucket = "lfymall";
    private static final String endpoint="oss-cn-fuzhou.aliyuncs.com";
    @Resource
    private MinioUtil minioUtil;
    @PostMapping("/up")
    public void up(MultipartFile file){
        OSS oss = new OSSClient(endpoint, accessKey, secretKey);
        String name = file.getOriginalFilename();
        String substring = name.substring(name.lastIndexOf("."));
        PutObjectResult res = null;
        try {
            InputStream inputStream = file.getInputStream();
            res = oss.putObject(bucket, "lfy/test"+substring, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(null !=res&&res.getResponse().getStatusCode()==200){
            System.out.println("上传成功");

        }else {
            System.out.println("上传失败");
        }



    }
    @SneakyThrows
    @GetMapping("/testup")
    public void testup(HttpServletRequest request, HttpServletResponse response){
       /* response.getOutputStream();
        File file = ResourceUtils.getFile("classpath:test.docx");
        XWPFTemplate template = XWPFTemplate.compile(file).render(
                new HashMap<String, Object>(){{
                    put("title", "Hi, poi-tl Word模板引擎");
                }});
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        OutputStream outputStream1 = new OutputStream();
//        template.writeAndClose();


        OSS oss = new OSSClient(endpoint, accessKey, secretKey);

        PutObjectResult res = null;

        res = oss.putObject(bucket, "lfy/test.docx",);
        if(null !=res&&res.getResponse().getStatusCode()==200){
            System.out.println("上传成功");

        }else {
            System.out.println("上传失败");
        }*/
    }
    @GetMapping("/")
    public String index(){
        return "index.html";
    }

    @GetMapping("/minio")
    @ResponseBody
    public void test(){

    }
    @GetMapping("/minup")
    @ResponseBody
    public String upmin(MultipartFile file){
        List<String> upload = minioUtil.upload(new MultipartFile[]{file});
        for (String s : upload) {
            System.out.println(s);
        }
        return upload.get(0);
    }
}
